package com.hq.fxh.No3_TuiGuang.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.hq.fxh.CustomerView.ChartView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetDayStatisticalResultRes;
import com.hq.fxh.Modle.GetWeekStatisticalResultRes;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Adaoter.ExportDetailFragmentAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;

/**
 * @author 小谢
 * @ClassName ExportDetailFragment
 * @Description :
 * @Created 2016/12/29 0029
 */
public class ExportDetailFragment extends BaseFragment {
    private TextView sum;
    private RecyclerView recyclerView;
    private GetWeekStatisticalResultRes.DataBean.CommonBean data;
    private int uid;
    private List<GetDayStatisticalResultRes.DataBean> employeeDayList = new ArrayList<>();
    ChartView chartView;

    private ExportDetailFragmentAdapter adapter;
    private int PageNum = 20;
    private int PageCurrent = 1;
    private int sort;

    public void setSort(int sort) {
        this.sort = sort;
    }

    //x轴坐标对应的数据
    private List<String> xValue = new ArrayList<>();
    //y轴坐标对应的数据
    private List<Integer> yValue = new ArrayList<>();
    //折线对应的数据
    private Map<String, Integer> value = new HashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_export_detail;
    }

    @Override
    protected void findView(View view) {
        sum = (TextView) view.findViewById(R.id.sum);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        chartView = (ChartView) view.findViewById(R.id.chartview);

    }

    @Override
    protected void initData() {
        uid = SharedPreferenceUtils.getMemberId(getActivity());

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter

    }

    private void netRequest(final String date, int pos) {
        //[sort:1曝光 2点击 3转发 4文章总数
        LogUtils.e("data:" + date);
        UserServiceImpl.getDayStatisticalResult(uid + "", date, pos, PageCurrent, PageNum, new NetworkSubscriber<GetDayStatisticalResultRes>() {
            @Override
            public int onSuccess(GetDayStatisticalResultRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    if (PageCurrent == 1) {
                        employeeDayList.clear();
                    }
                    employeeDayList.addAll(data.getData());

                    if (adapter == null) {
                        adapter = new ExportDetailFragmentAdapter(getActivity(), employeeDayList);
                        recyclerView.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    LogUtils.startToash(getActivity(), data.getMsg());
                }
                return 0;
            }
        });
    }

    public void setData(int position, GetWeekStatisticalResultRes.DataBean.CommonBean data) {
        final List<GetWeekStatisticalResultRes.DataBean.CommonBean.CommonDataBean> dataList = data.getData();
        int max = dataList.get(0).getValue();
        int min = dataList.get(0).getValue();
        for (int i = 0; i < dataList.size(); i++) {
            if (max < dataList.get(i).getValue()) {
                max = dataList.get(i).getValue();
            }
            if (min > dataList.get(i).getValue()) {
                min = dataList.get(i).getValue();
            }

            String substring = dataList.get(i).getTime().substring(dataList.get(i).getTime().indexOf("-") + 1);
            xValue.add(substring);
            value.put(substring, dataList.get(i).getValue());
        }
        int div = (max - min) / 16;
        min = min - (2 * div);
        for (int y = 0; y < 24; y++) {
            yValue.add(min + (div * (y + 1)));
        }
        chartView.setSort(sort);
        chartView.setValue(value, xValue, yValue);
        chartView.setOnChartClickListener(new ChartView.OnChartClickListener() {
            @Override
            public void onClick(int position, Integer data) {
                LogUtils.e(position + "    " + data.toString());
                netRequest(dataList.get(position).getTime(), sort);

            }
        });

        netRequest(dataList.get(0).getTime(), sort);
        // createChildLinearLayout(position, dataList, max);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            JAnalyticsInterface.onPageStart(getContext(), "近7日数据");
        } else {
            JAnalyticsInterface.onPageEnd(getContext(), "近7日数据");
        }
    }

    TextView tempTextView;//临时变量
    ImageView tempImage;//临时变量
    ImageView tempData;


    public void setTextView(String str) {
        sum.setText(str);
    }

}
